﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.IBase.DBHelper;
using yq.CacheHelp;

namespace yq.IBase
{
    /// <summary>
    /// 数据库执行操作委托
    /// 如果涉及缓存同步请重写
    /// </summary>
    public class DbExec
    {
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <param name="doFunc"></param>
        /// <returns></returns>
        public virtual F DoRead<F>(Func<DbHelper, F> doFunc)
        {
            DateTime openTime = DateTime.Now;
            using (DbHelper helper = new DbHelper())
            {
                try
                {
                    var val = doFunc(helper);
                    
                    //日志
                    Log(helper.SqlLog, DateTime.Now - openTime);

                    return val;
                }
                catch (Exception ex)
                {
                    //日志
                    Log(helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());
                    throw;
                }
            }
        }

        /// <summary>
        /// 执行事物
        /// </summary>
        /// <param name="doFunc"></param>
        public virtual void DoTrans(Action<DbHelper, CaCheRefresh> doFunc)
        {
            DateTime openTime = DateTime.Now;
            using (DBTrans ts = new DBTrans())
            {
                DbHelper helper = new DbHelper(ts);
                try
                {
                    doFunc(helper, helper.CaCheRefresh);

                    //防止未提交事物引起的异常
                    if (helper.TransType == TransType.Open)
                    {
                        DoCaCheRefresh(helper.CaCheRefresh.Get());
                        helper.Commit();
                    }

                    //日志
                    Log(helper.SqlLog, DateTime.Now - openTime);
                }
                catch (Exception ex)
                {
                    helper.Rollback();

                    //日志
                    Log(helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());

                    DoTryCaCheRefresh(helper.CaCheRefresh.Get());
                    throw;
                }
            }
        }

        /// <summary>
        /// 执行事物
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <param name="dofunc"></param>
        /// <returns></returns>
        public virtual F DoTrans<F>(Func<DbHelper, CaCheRefresh, F> doFunc)
        {
            DateTime openTime = DateTime.Now;
            using (DBTrans ts = new DBTrans())
            {
                DbHelper helper = new DbHelper(ts);
                try
                {
                    var outval = doFunc(helper, helper.CaCheRefresh);

                    //防止未提交事物引起的异常
                    if (helper.TransType == TransType.Open)
                    {
                        DoCaCheRefresh(helper.CaCheRefresh.Get());
                        helper.Commit();
                    }
                    Log(helper.SqlLog, DateTime.Now-openTime);

                    return outval;
                }
                catch(Exception ex)
                {
                    helper.Rollback();

                    //日志
                    Log(helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());

                    DoTryCaCheRefresh(helper.CaCheRefresh.Get());
                    throw;
                }

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="list">SQL语句列表</param>
        /// <param name="tp">执行间隔时间</param>
        /// <param name="errorMsg">异常消息，如果有</param>
        protected virtual void Log(IList<string> list, TimeSpan tp, string errorMsg = "")
        {
            Console.WriteLine("执行语句：");
            foreach (var sql in list)
            {
                Console.WriteLine(sql);
            }
            Console.WriteLine("用时:" + tp.TotalMilliseconds+"毫秒");
            Console.WriteLine("异常信息:" + (errorMsg.Length == 0 ? "无" : errorMsg) );
        }

        /// <summary>
        /// 正常执行更新缓存方法
        /// </summary>
        /// <param name="list"></param>
        protected virtual void DoCaCheRefresh(IList<FullAndId> list)
        {

        }

        /// <summary>
        /// 异常时直接清理缓存，以免出现数据差异
        /// </summary>
        /// <param name="list"></param>
        protected virtual void DoTryCaCheRefresh(IList<FullAndId> list)
        {

        }
    }
}
